﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Org;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.RelationshipTransfer
{
    public partial class TWord : PageBase
    {

        public string RelationID
        {
            get
            {
                return this.Request["RelationID"] ?? "";
            }
        }


        public string mode
        {
            get
            {
                return this.Request["mode"] ?? "";
            }
        }
        public DataTable ProcessInfo
        {
            get
            {
                return PartyRelationshipTransfer.GetProcessesView(this.RelationID);
            }
        }

        public PartyRelationshipTransfer TransferModel
        {
            get;
            set;
        }

        //介绍信发出单位
        public string LetterFrom { get; set; }

        //介绍信接收单位
        public string LetterTo { get; set; }


        public PartyOrgMember OrgMemberModel
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            WordBusiness.Export(this, palWord, "组织关系转接.doc");
        }

        /// <summary>
        /// 加载申请人信息
        /// </summary>
        public void LoadData()
        {
            TransferModel = DB.PartyRelationshipTransfers.Find(this.RelationID);
            OrgMemberModel = DB.PartyOrgMembers.Find(TransferModel.Applicant);

            //
            var branchID = this.CurrentUser.BranchID;
            if (branchID == TransferModel.FormerBranchID)//如果当前单位为发出单位则发出为本单位，接收为本单位上级党委
            {
                LetterFrom = branchID;
                LetterTo = DB.PartyOrgBranchs.Find(branchID).ParentBranchID;
            }
            else if (branchID == TransferModel.TageBranchID)
            {
                LetterTo = branchID;
                var parentToBranchId = DB.PartyOrgBranchs.Find(TransferModel.TageBranchID).ParentBranchID;
                if (parentToBranchId == "D658070EC3BF425BA8417745FDCBFCB0")//
                {
                    LetterFrom = DB.PartyOrgBranchs.Find(TransferModel.FormerBranchID).ParentBranchID;
                }
                else
                {
                    LetterFrom = parentToBranchId;
                }
            }
            else//如果当前单位不是发出支部，则发出单位为发出支部的上级党委；接收单位为接收支部的上级党委
            {
                var parentFrom = DB.PartyOrgBranchs.Find(TransferModel.FormerBranchID).ParentBranchID;
                if (branchID == parentFrom)
                {
                    LetterFrom = branchID;

                    var toparentid = DB.PartyOrgBranchs.Find(TransferModel.TageBranchID).ParentBranchID;
                    if (branchID == toparentid)//党委内部转移
                    {
                        LetterTo = TransferModel.TageBranchID;
                    }
                    else
                    {
                        LetterTo = toparentid;
                    }

                }
                else
                {
                    LetterFrom = branchID;
                    LetterTo = TransferModel.TageBranchID;
                }
            }
        }

        public string GetMemberCost(int index)
        {
            if (string.IsNullOrEmpty(TransferModel.MemberCostTime))
            {
                return "";
            }
            string[] temp = TransferModel.MemberCostTime.Split('-');
            if (temp.Length != 2)
            {
                return "";
            }
            return temp[index];
        }

        public string GetPoliticsLineamentName(string value)
        {
            DropDownList list = new DropDownList();
            ControlBusiness.ControlBindDataDictionary(list, "ZHDJ.Member.PoliticsLineament");
            list.SelectedValue = value;
            return list.Items[list.SelectedIndex].Text.Replace("党员", "");
        }
    }
}